
import { Pipe, PipeTransform, Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError, switchMap } from "rxjs/operators";
import { HttpClient } from '@angular/common/http';
import { DomSanitizer} from '@angular/platform-browser';
export function createTranslateLoader(http: HttpClient,version:string) {
  let versionSuffix = "";
  if (version) {
    versionSuffix = "?v=" + version;
  }
  return new TranslateHttpLoader(http, '/apps/apporder/df/web/bo-employee-front/employeeform/i18n/', '.json'+ versionSuffix);
}

export let lang = {"zh-CHS":{"root-component":"","root-layout":"","page-header":"","header-nav":"","header-title-container":"","page-header-title":"","page-header-toolbar":"","button-add":"新增","button-edit":"编辑","button-save":"保存","button-cancel":"取消","main-container":"","like-card-container":"","basic-form-component-ref":"","detail-container":"","detail-section":"","Section/detail-section/mainTitle":"","Section/detail-section/subTitle":"","detail-tab":"","jobinfo-tab-page":"工作信息","jobinfo-component-ref":"","jobinfo-tab-toolbar":"","jobinfoAddButton":"新增","jobinfoRemoveButton":"删除","basic-form-component":"","basic-form-section":"","Section/basic-form-section/mainTitle":"基本信息","Section/basic-form-section/subTitle":"","basic-form-layout":"","code_7b2f03f5_6dgt":"编号","TextBox/code_7b2f03f5_6dgt/placeHolder":"","name_8b788a59_326c":"名称","TextBox/name_8b788a59_326c/placeHolder":"","remark_87a8db9b_m2hn":"备注","TextBox/remark_87a8db9b_m2hn/placeHolder":"","state_f6094b3b_m7k9":"状态","TextBox/state_f6094b3b_m7k9/placeHolder":"","organization_efd7f305_vfoh":"组织","TextBox/organization_efd7f305_vfoh/placeHolder":"","gender_e593e1a1_ewup":"性别","TextBox/gender_e593e1a1_ewup/placeHolder":"","countryOrArea_0f822ec8_5rxf":"所属国家或地区","TextBox/countryOrArea_0f822ec8_5rxf/placeHolder":"","location_411a7c80_rtuz":"常驻地","TextBox/location_411a7c80_rtuz/placeHolder":"","postID_d9085102_xlsc":"职级","TextBox/postID_d9085102_xlsc/placeHolder":"","category_7c8c1521_gi63":"人员类别","TextBox/category_7c8c1521_gi63/placeHolder":"","displayName_ed752cbf_gayz":"显示姓名","TextBox/displayName_ed752cbf_gayz/placeHolder":"","telephone_00be3451_drfr":"联系电话","TextBox/telephone_00be3451_drfr/placeHolder":"","secret_a77111d8_hjht":"密级","TextBox/secret_a77111d8_hjht/placeHolder":"","mailbox_a9822261_vy64":"邮箱","TextBox/mailbox_a9822261_vy64/placeHolder":"","jobinfo-component":"","jobinfo-component-layout":"","dataGrid_jobinfo":"","DataGrid/dataGrid_jobinfo/lineNumberTitle":"","DataGrid/dataGrid_jobinfo/OperateEditButton":"编辑","DataGrid/dataGrid_jobinfo/OperateDeleteButton":"删除","DataGrid/dataGrid_jobinfo/OperateColumn":"操作","company_ea7589cc_eq6f":"工作单位","GridField/company_ea7589cc_eq6f/editor/company_ea7589cc_jefs":"文本","GridField/company_ea7589cc_eq6f/editor/TextBox/company_ea7589cc_jefs/placeHolder":"","timeInterval_402bb43e_h45i":"时间区间","GridField/timeInterval_402bb43e_h45i/editor/timeInterval_402bb43e_l6t2":"文本","GridField/timeInterval_402bb43e_h45i/editor/TextBox/timeInterval_402bb43e_l6t2/placeHolder":"","position_72b50570_bpin":"岗位","GridField/position_72b50570_bpin/editor/position_72b50570_fw9q":"文本","GridField/position_72b50570_bpin/editor/TextBox/position_72b50570_fw9q/placeHolder":"","salary_43c48ecf_g89o":"薪资","GridField/salary_43c48ecf_g89o/editor/salary_43c48ecf_calg":"复选框","GridField/salary_43c48ecf_g89o/formatter/trueText":"是","GridField/salary_43c48ecf_g89o/formatter/falseText":"否","certifier_1e447038_l1vb":"证明人","GridField/certifier_1e447038_l1vb/editor/certifier_1e447038_w5bj":"文本","GridField/certifier_1e447038_l1vb/editor/TextBox/certifier_1e447038_w5bj/placeHolder":"","telephone_0a01ff5e_3zek":"证明人联系方式","GridField/telephone_0a01ff5e_3zek/editor/telephone_0a01ff5e_dk6o":"文本","GridField/telephone_0a01ff5e_3zek/editor/TextBox/telephone_0a01ff5e_dk6o/placeHolder":""}};

@Pipe({ name: 'lang' })
export class LangPipe implements PipeTransform {
  constructor(private translate: TranslateService, private http: HttpClient) { }
  transform(key: string, langCode: string, defaultValue?: string) {
      
    const translateValue = this.translate.instant(key);
    if (translateValue == "JitI18nDefaultValue") {
      return defaultValue ? defaultValue : "";
    }

    return translateValue;
  }
}
@Pipe({ name: 'safeHtml' })
export class SafeHtmlPipe implements PipeTransform {
  constructor(private sanitizer: DomSanitizer) { }
  transform(url) {
    if (!url) {
      url = "";
    }
    return this.sanitizer.bypassSecurityTrustResourceUrl(url);
  }
}
@Injectable()
export class LangService {
  constructor(private translate: TranslateService) { }
  transform(key: string, langCode: string, defaultValue?: string) {
    
    const translateValue = this.translate.instant(key);
    if (translateValue == "JitI18nDefaultValue") {
      return defaultValue ? defaultValue : "";
    }

    return translateValue;
  }

  getCurrentLanguage() {
    return this.translate.currentLang;
  }

}

@Injectable()
export class TranslateResolveService implements Resolve<any>{

  constructor(private translate: TranslateService, private http: HttpClient) {
    translate.defaultLang = 'zh-CHS';
    translate.setTranslation('zh-CHS', lang['zh-CHS']);
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {
    let langCode = localStorage.getItem('languageCode');
    if (!langCode) {
      langCode = "zh-CHS";
    }
    if (langCode == "zh-CHS" || (this.translate.defaultLang === langCode && this.translate.currentLoader == createTranslateLoader(this.http,null))) {
      this.translate.setTranslation('zh-CHS', lang['zh-CHS']);
      return of(this.translate[langCode]);
    } else {
      const httpOb = this.http.get("/apps/apporder/df/web/bo-employee-front/version.json?v=" + new Date().getTime()).pipe(switchMap((data)=>{
        let currentVersion = null;
        if (data instanceof Array) {
          const versionKey = "employeeform/" + langCode + ".json";
          data.forEach((item) => {
            if (item.category == "i18n" && item.key == versionKey) {
              currentVersion = item.value;
            }
          });
        }

        this.translate.defaultLang = langCode;
        this.translate.currentLang = langCode;
        this.translate.currentLoader = createTranslateLoader(this.http, currentVersion);

    let tran = this.translate.getTranslation(langCode).pipe(catchError(err => {
      console.error("read resource file failed,please check!!! "+ err);
      return of(err);
    }));
    return tran;
      }));
      return httpOb;
    }
  }
}
